home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5265 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: news1.h1.usa.pipeline.com!usenet
  2. From: grantp@usa.pipeline.com(Pete)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Q Re: struct and functions
  5. Date: 3 Feb 1996 10:01:18 GMT
  6. Organization: Kalevi, Inc.
  7. Message-ID: <4evbpe$btq@news1.usa.pipeline.com>
  8. NNTP-Posting-Host: 38.8.120.9
  9. X-PipeUser: grantp
  10. X-PipeHub: usa.pipeline.com
  11. X-PipeGCOS: (Pete)
  12. X-Newsreader: Pipeline USA v3.3.0
  13.  
  14. On Feb 03, 1996 09:18:27 in article <Q Re: struct and functions>,
  15. 'martp@oanet.com' wrote: 
  16.  
  17.  
  18. >Hi 
  19. >I'm a second semister student in Computer Systems, and have a question 
  20. >about passing structs, for Win programming. 
  21.  
  22. It's the same in Windows and in character-mode systems. 
  23.  
  24. >I have a struct DATA containing, amongst other things, HWND, and 
  25. >COLORREF. 
  26. >How do I define, call, and make a header for a function,  so that I 
  27. >can just pass the DATA to the function, and separate the HWND and 
  28. >COLORREF in the header? 
  29. >struct DATA { 
  30. >HWND hWnd; 
  31. >COLORREF cColor; 
  32. >}stData; 
  33. The problem with the above is that it actually instantiates the 
  34. structure.  If you include this header in multiple source files -- 
  35. which you nearly always do in "real" programs -- then you 
  36. get multiple definition linker errors.  Better to change it to: 
  37. (You posted this to comp.lang.c++, but judging by your 
  38. code, you want a "straight C" answer.): 
  39.  
  40. typedef struct tagDATA 
  41.  { 
  42.     HWND hWnd; 
  43.     COLORREF cColor; 
  44.  } DATA; 
  45.  
  46. >void fnChangeData( DATA stData ); 
  47. >fnChangeColor( stData ); 
  48.  
  49. Change the above line to: 
  50. void fnChangeColor(DATA * pdata); 
  51.  
  52. All of the above goes into a header file.  From here on down, 
  53. into a .c file: 
  54. >void fnChangeColor( HWND hWnd, COLORREF cColor ) 
  55. No, this doesn't match the declaration in the header.  Define 
  56. it just like its declaration: 
  57.  
  58. void fnChangeColor(DATA * data) 
  59.  
  60. >{ 
  61. Now, inside this function you refer to elements of the 
  62. structure using pointer syntax; e.g., 
  63.  
  64.     HDC hDC = GetDC(data->hWnd); 
  65.     SetTextColor(hDC, data->cColor); 
  66.  
  67. >return; 
  68. Not necessary. 
  69. >} 
  70.  
  71. To call this mess, do something like: 
  72.  
  73. int foo (HWND hw) 
  74.  { 
  75.     DATA d;  /* this reserves memory on the stack for the struct */ 
  76.     /* of course you could initialize the struct with . = { hw, RGB(255, 0,
  77. 0) };*/ 
  78.     d.hWnd = hw; 
  79.     d.cColor = RGB(255, 0, 0); 
  80.     fnChangeColor(&d);  /* pass the address of your structure */ 
  81.  
  82. -- 
  83. Pete Grant 
  84. Kalevi, Inc. 
  85. Object Oriented Software Development
  86.